package zhuzhuwang;

import hb.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AskRentOrderController extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request,
			HttpServletResponse response)
			throws ServletException,IOException{
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession(true);
		String address="/index.jsp";
		String action=(request.getParameter("action")).trim();
		
		if(action.equals("makeAnOrder")){
			
			if((session.getAttribute("user")!=null)
					&&(session.getAttribute("status")=="1")){
				AskRentOrder temp=new AskRentOrder();
				temp.setDate(new java.util.Date());

				temp.setUser((new UserDao()).getUser((String)session.getAttribute("user")));

				AskRentList temp1=(AskRentList)session.getAttribute("need");
				temp1.setStatus(1);
				(new AskRentListDao()).update(temp1);
				temp.setAskRentList(temp1);
				
				
				(new AskRentOrderDao()).insert(temp);
				address="/userspace.jsp";
			}
			else{
				address="/login.jsp";
			}
				
		}
		
		
		if(action.equals("confirmOrder")){
			AskRentOrder askRentOrder=(new AskRentOrderDao()).getAskRentOrder((request.getParameter("orderID")).trim());
			AskRentList askRentHouse=askRentOrder.getAskRentList();
			askRentHouse.setStatus(2);
			(new AskRentListDao()).update(askRentHouse);
			
			askRentOrder.setAskRentList(askRentHouse);
			
			(new AskRentOrderDao()).update(askRentOrder);
			
			List askRentList=(new AskRentListDao()).getAll();
			session.setAttribute("askRentList", askRentList);
			List AskRentOrderList=(new AskRentOrderDao()).getAll();
			session.setAttribute("askRentOrder", AskRentOrderList);
			
			address="/order.jsp";
		}
		
		if(action.equals("deleteOrder")){
			AskRentOrder askRentOrder=(new AskRentOrderDao()).getAskRentOrder((request.getParameter("orderID")).trim());
			AskRentList askRentHouse=askRentOrder.getAskRentList();
			askRentHouse.setStatus(0);
			(new AskRentListDao()).update(askRentHouse);
			
			askRentOrder.setAskRentList(askRentHouse);
			
			(new AskRentOrderDao()).delete(askRentOrder);
			
			List askRentList=(new AskRentListDao()).getAll();
			session.setAttribute("askRentList", askRentList);
			List AskRentOrderList=(new AskRentOrderDao()).getAll();
			session.setAttribute("askRentOrder", AskRentOrderList);
			
			
			address="/order.jsp";
		}
		
		if(action.equals("deleteOrderByUser")){
			AskRentOrder askRentOrder=(new AskRentOrderDao()).getAskRentOrder((request.getParameter("orderID")).trim());
			AskRentList askRentHouse=askRentOrder.getAskRentList();
			askRentHouse.setStatus(0);
			(new AskRentListDao()).update(askRentHouse);
			
			askRentOrder.setAskRentList(askRentHouse);
			
			(new AskRentOrderDao()).delete(askRentOrder);
			
			address="/userspace.jsp";
		}
		
		RequestDispatcher dispatcher=request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
